import router from "@/router";
import store from "@/store";
import ACCESS_ENUM from "./accessEnum";
/**
 * 每次路由改变之前都去验证权限，
 */
router.beforeEach(async (to, from, next) => {
  console.log("logined user info", store.state.user.loginUser);
  const loginUser = store.state.user.loginUser;

  if (!loginUser || !loginUser.userRole) {
    await store.dispatch("user/getLoginUser");
  }

  const needAccess = (to.meta?.access as string) ?? ACCESS_ENUM.NOT_LOGIN;

  if (needAccess !== ACCESS_ENUM.NOT_LOGIN) {
    if (!loginUser || !loginUser.userRole) {
      next(`/user/login?redirect=${to.fullPath}`);
      return;
    }
  }
  next();
});
